package cn.jee.cs.model;

import java.time.LocalDate;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.*;
import javax.validation.constraints.Email;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Null;
import javax.validation.constraints.Past;
import javax.validation.constraints.Pattern;
import javax.validation.constraints.Positive;
import javax.validation.constraints.Size;

import com.fasterxml.jackson.annotation.JsonBackReference;
import com.fasterxml.jackson.annotation.JsonIgnore;
import org.springframework.format.annotation.DateTimeFormat;

import cn.jee.cs.model.validation.OnCreate;
import cn.jee.cs.model.validation.OnEdit;

@Entity
public class Student {
  @Id
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  @Null(groups = OnCreate.class, message="添加学生时不需要提供Id")
  @NotNull(groups = OnEdit.class, message="更新时未发现学生Id")
  @Positive
  Long id;
  
  @Pattern(regexp = "^2\\d{7}$", message="学号为2开头的8位数字")
  String sno;
  
  @NotBlank
  @Size(min=2,max=20)
  String firstname;
  
  @NotBlank
  @Size(min=2,max=20)
  String lastname;
  
  @Past
  @DateTimeFormat(pattern = "yyyy-MM-dd")
  LocalDate birthday;
  
  @NotNull
  @Pattern(regexp="^M|F$", message="性别只能是M或者F")
  String gender;
  
  @Email
  String email;

//  @ManyToMany(mappedBy = "students")
//  @JsonBackReference
//  private Set<Course> courses = new HashSet<>();

  @OneToMany(mappedBy = "student")
  @JsonIgnore
  Set<CourseStudent> courseStudents = new HashSet<>();

  public Set<CourseStudent> getCourseStudents() {
    return courseStudents;
  }

  public void setCourseStudents(Set<CourseStudent> courseStudents) {
    this.courseStudents = courseStudents;
  }

//  public Set<Course> getCourses() {
//    return courses;
//  }
//
//  public void setCourses(Set<Course> courses) {
//    this.courses = courses;
//  }
//
//  // 当前学生添加一个课程
//  public void addCourse(Course course){
//    this.courses.add(course);
//  }
//
//  // 当前学生移除一个课程
//  public void removeCourse(Course course){
//    this.courses.remove(course);
//  }
//
//  /**
//   * 添加多个课程
//   * @param courses
//   */
//  public void addCourses(Collection<Course> courses){
//    this.courses.addAll(courses);
//  }
//
//  /**
//   * 移除多个课程
//   * @param courses
//   */
//  public void removeCourses(Collection<Course> courses){
//    this.courses.removeAll(courses);
//  }

  public String getEmail() {
    return email;
  }

  public void setEmail(String email) {
    this.email = email;
  }

  public Student(Long id, String sno, String firstname, String lastname, String gender) {
    super();
    this.id = id;
    this.sno = sno;
    this.firstname = firstname;
    this.lastname = lastname;
    this.gender = gender;
  }

  public Student() {
  }

  public Long getId() {
    return this.id;
  }

  public void setId(Long id) {
    this.id = id;
  }

  public String getSno() {
    return sno;
  }

  public void setSno(String sno) {
    this.sno = sno;
  }

  public String getFirstname() {
    return firstname;
  }

  public void setFirstname(String firstname) {
    this.firstname = firstname;
  }

  public String getLastname() {
    return lastname;
  }

  public void setLastname(String lastname) {
    this.lastname = lastname;
  }

  public LocalDate getBirthday() {
    return birthday;
  }

  public void setBirthday(LocalDate birthday) {
    this.birthday = birthday;
  }

  public String getGender() {
    return gender;
  }

  public void setGender(String gender) {
    this.gender = gender;
  }

}
